<?php
/**
 * 文件系统操作(FileSystem Operator)
 *
 * Created		: 2011-07-22
 * Modified		: 2011-07-22
 * @link		: http://www.1-wg.com
 * @copyright	: [Qixbo.Com] (c) 2011
 * @version		: 0.1.0
 * @author		: Joseph Chen (jsph.chen@gmail.com)
 */
class Fso
{
	/**
	 * mkdir 的扩展,可以递归创建目录
	 * @param $path 设置的目录
	 * @param $mode 目录的模式
	 * @return boolean
	 */
	public static function mkdir($path, $mode=0775)
	{
		if (is_dir($path))
		{
			return true;
		}
		mkdir($path, $mode, true);
		return $path;
	}

	/**
	 * 获取文件的后缀名
	 * @param string $filename
	 */
	public static function getExtension($filename)
	{
		return pathinfo($filename, PATHINFO_EXTENSION);
	}

	/**
	 * 将数据写入文件
	 * @param $file 文件名
	 * @param $writetext 写入文件的数据
	 * @param $filetype 文件类型：text | php
	 * @param $openmod 打开文件的模式
	 * @param $exit 写入错误是否退出程序
	 * @return boolean
	 */
	public static function write($file, $writetext, $filetype='text', $openmod='w')
	{
		self::mkdir(dirname($file));
		if(!$fp = fopen($file, $openmod))
		{
			return false;
		}

		$text = '';

		if($filetype == 'php')
		{
			$text = "<?php\r\n";
		}

		$text .= $writetext;
		if ($filetype == 'php' && substr($text, -1) != ';') {
			$text .= ';';
		}

		flock($fp, LOCK_EX);
		fwrite($fp, $text);
		flock($fp, LOCK_UN);
		fclose($fp);

		return true;
	}

	/**
	 * 递归获取目录及其子目录
	 * @param string $dir 要读取的目录
	 * @param int $recursion 限制递归的层数
	 */
	public static function getDirs($dir, $exclude='', $recursion=100)
	{
		static $recur = 0;
		if ($recursion > 0 && $recur >= $recursion) {
			return $dir;
		}

		$list = array();
		if (!is_dir($dir)) {
			return $list;
		}

		if ($exclude) {
			$exclude = explode('|', $exclude);
		} else {
			$exclude = array();
		}

		$d = dir($dir);
		while (false !== ($entry = $d->read())) {
			if ($entry == '.' || $entry == '..' || in_array($entry, $exclude)) {
				continue;
			}
			$endChar = substr($dir, -1, 1);
			if ($endChar == DIRECTORY_SEPARATOR || $endChar == '/') {
				$newDir = $dir . $entry;
			} else {
				$newDir = $dir . DIRECTORY_SEPARATOR . $entry;
			}
			if (is_dir($newDir)) {
				$list[] = $newDir;
				$subList = self::getDirs($newDir);
				if (is_array($subList)) {
					$list = array_merge($list, $subList);
				} else {
					$list[] = $subList;
				}
			}
		}

		$d->close();

		$recur ++;

		return $list;
	}

	/**
	 * 递归获取目录及其子目录下的所有文件
	 * @param string $dir 要读取的目录
	 * @param int $recursion 限制递归的层数
	 */
	public static function getDirFiles($dir, $exclude='', $recursion=100)
	{
		static $recur = 0;
		if ($recursion > 0 && $recur >= $recursion) {
			return array();
		}

		$recur ++;

		$list = array();
		if (!is_dir($dir)) {
			return $list;
		}

		if ($exclude) {
			$excludeArr = explode('|', $exclude);
		} else {
			$excludeArr = array();
		}

		$d = dir($dir);
		while (false !== ($entry = $d->read())) {
			if ($entry == '.' || $entry == '..' || in_array($entry, $excludeArr)) {
				continue;
			}
			$endChar = substr($dir, -1, 1);
			if ($endChar == DIRECTORY_SEPARATOR || $endChar == '/') {
				$newFile = $dir . $entry;
			} else {
				$newFile = $dir . DIRECTORY_SEPARATOR . $entry;
			}
			if (is_dir($newFile)) {
				$list[] = $newFile;
				$subList = self::getDirFiles($newFile, $exclude, $recursion);
				if (is_array($subList)) {
					$list = array_merge($list, $subList);
				} else {
					$list[] = $subList;
				}
			} else if (is_file($newFile)) {
				$list[] = $newFile;
			}
		}

		$d->close();

		return $list;
	}

	/**
	 * 递归删除目录及其中所有的文件
	 * @param $dir 要删除的目录
	 */
	public static function rmdir($dir)
	{
		if (!is_dir($dir)) {
			return true;
		}
		$d = dir($dir);

		while (false !== ($entry = $d->read())) {
			if ($entry == '.' || $entry == '..') {
				continue;
			}
			$endChar = substr($dir, -1, 1);
			if ($endChar == DIRECTORY_SEPARATOR || $endChar == '/') {
				$newDir = $dir . $entry;
			} else {
				$newDir = $dir . '/' . $entry;
			}
			if (is_dir($newDir)) {
				$subList = self::rmdir($newDir);
			} else {
				unlink($newDir);
			}
		}

		$d->close();

		rmdir($dir);

		return true;
	}
	
	/**
	 * 复制目录
	 * @param string $sourceDir 要复制的源目录
	 * @param string $destDir 复制的目标目录
	 * @param string $exclude 不复制的目录及文件
	 * @param boolean $recursive 是否递归复制子目录
	 */
	public static function copyDir($sourceDir, $destDir, $exclude='', $recursive=true) 
	{
		$list = self::getDirFiles($sourceDir, $exclude);
		foreach ($list as $file) {
			$_file = strtr($file, '\\', '/');
			$_file = strtr($_file, array(strtr($sourceDir, '\\', '/') => ''));
			$_file = $destDir.$_file;
			if (is_dir($file)) {
				Fso::mkdir($_file);
				continue;
			}
			copy($file, $_file);
		}
		return true;
	}
	
	/**
	 * 获取一张图片的后缀名
	 * @param string $file
	 */
	public static function getImageExt($file) 
	{
		if ($type = exif_imagetype($file)) {
			switch ($type) {
				case IMAGETYPE_GIF:
					return '.gif';
					
				case IMAGETYPE_JPEG:
					return '.jpg';
					
				case IMAGETYPE_PNG:
					return '.png';
				
				default:
					return false;;
			}
		} else {
			return false;
		}
	}
}
